package com.caul.demo.hadoop.mapreduce.attention;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by sdliang on 2018/4/8.
 */
public class FirstAttentionReducer extends Reducer<Text, Text, Text, Text> {

  private static Text textKey;

  @Override
  protected void setup(Context context) throws IOException, InterruptedException {
    textKey = new Text();
  }

  @Override
  protected void reduce(Text key, Iterable<Text> values, Context context)
      throws IOException, InterruptedException {
    List<String> attents = new ArrayList<>();
    for (Text value : values) {
      attents.add(value.toString());
    }
    if (attents.size() <= 1) {
      return;
    }
    //排序
    Collections.sort(attents);

    for (int i = 0; i < attents.size() - 1; i++) {
      for (int k = i + 1; k < attents.size(); k++) {
        textKey.set(attents.get(i) + "-" + attents.get(k));
        context.write(textKey, key);
      }
    }
  }
}
